﻿
<!DOCTYPE html>

<html class="x-admin-sm">
<head>
    <meta name="viewport" content="width=device-width" />
    <meta charset="UTF-8">
    <title>菜单权限设置</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link href="~/X-admin/css/font.css" rel="stylesheet" />
    <script src="~/X-admin/lib/layui/layui.js"></script>
    <link href="~/X-admin/css/xadmin.css" rel="stylesheet" />
    <script src="~/X-admin/js/jquery.min.js"></script>
    <script src="~/X-admin/js/xadmin.js"></script>
    <script src="~/js/framework/sysframework.js"></script>
</head>
<body>
    <ul lay-bar="disabled" class="mWebBanner">
        <li class="layui-nav-item" style="float:left;">
            <button id="btn-update" type="button" name="btn_update" onclick="saveMenuPower()" class="buttonCss">
                 <i class="layui-icon" style="margin-right:3px">&#x1005;</i> 保 存
            </button>
        </li>
        <li class="layui-nav-item" style="float:left;margin-left:3%">
            <button id="btn-sync" type="button" onclick="closeWin()" class="buttonCss">
                 <i class="layui-icon" style="margin-right:3px">&#x1007;</i> 关 闭
            </button>
        </li>
    </ul>
    <div class="layui-card" style="margin-top:10px">
        <div class="layui-card-body ">
            <form class="layui-form" id="qForm">
              <div class="layui-inline" >
                    <select id="pType" name="pType" lay-filter="qpt">
                    <option value="">权限类型</option>
                </select>
               </div>
                <div class="layui-inline">
                    <input type="text" id="pCode" name="pCode" placeholder="权限编码" class="layui-input">
                </div>
                  <div class="layui-inline">
                    <input type="text" id="pName" name="pName" placeholder="权限名称" class="layui-input">
                </div>
               <input type="hidden" id="menuCode" name="menuCode" />
                <div class="layui-inline">
                    <button type="button" class="layui-btn layui-btn-sm " onclick="loadData()">
                        <i class="layui-icon" style="margin-right:3px">&#xe615;</i>查询
                    </button>
                </div>
                <input type="hidden" name="pUse" id="pUse" value="web" />
            </form>
         
          
        <div style="margin-left:3px">
            <table class="layui-hide" id="list" lay-filter="list"></table>

        </div>
    </div>
    </div>
</body>
</html>
<script src="~/js/framework/commonjs.js"></script>
<script type="text/html" id="barDemo">
    <a title="删除" href="javascript:;" lay-event="del" id="del"><i class="layui-icon">&#xe640;</i></a>
</script>
<script type="text/javascript">
    var mCode = "";
    $(function () {
        mCode = getUrlVariable("menuCode");
        $("input[ name='menuCode']").val(mCode);
        DictionaryDropDown("pwt", "pType", "");
        loadData();
        $(document).keyup(function (event) {
            if (event.keyCode == 13) {
                loadData();
            }
        })
       layui.use('form', function () {
            var form = layui.form;
            form.on('select(qpt)', function (data) {
                loadData();
            });
        });
    });


    /**
      * 系统权限列表
      **/
    function loadData() {
        var datas = JSON.stringify(FormToJson("qForm"));
        layui.use('table', function () {
            var table = layui.table;
            table.render({
                elem: '#list'
                , cols: [[ //标题栏
                    { type: 'checkbox' }
                    , { field: 'pCode', title: '权限编码', width: 100 }
                    , { field: 'pName', title: '权限名称', width: 100 }
                    , { field: 'wpName', title: '页面名称', width: 130,edit: 'text' }
                    , { field: 'wpOrder', title: '权限序号', width: 100, edit: 'text' }
                    , { field: 'wpEvent', title: '页面事件', width: 130, edit: 'text' }
                    , { field: 'wpIcon', title: '权限图标', width: 110, edit: 'text' }
                    , { field: 'right', title: '数据操作', toolbar: '#barDemo',width: 100, }
                ]], done: function (res, curr, count) {
                    $.each(res['data'], function (i, obj) {
                        if (obj["wpPower"] == "1") {
                            obj["LAY_CHECKED"] = 'true';
                            var index = obj['LAY_TABLE_INDEX'];
                            $('tr[data-index=' + index + '] input[type="checkbox"]').prop('checked', true);
                            $('tr[data-index=' + index + '] input[type="checkbox"]').next().addClass('layui-form-checked');
                        }
                    });
                }
                , height: $(document).height() - $('#list').offset().top - 40
                , url: "../System/getDataList?dataType=sysPower&condition=" + datas
                , skin: 'row' //表格风格
                , even: true
                , page: true //是否显示分页
                , limits: [20, 35, 50]
                , limit: 20 //每页默认显示的数量
            });
            //监听工具条
            table.on('tool(list)', function (obj) {
                var data = obj.data;
                if (obj.event == 'del') {
                    layer.confirm('确认要删除该权限？', function (index) {
                        DelWebPower(data.wpId);
                        layer.close(index);
                    });
                } 
            });
            //监听行工具事件
            table.on('edit(list)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data;
                SaveSinglePower(data.wpId, obj.field, obj.value);
            });
        });

    }


    /**
     * 保存信息
     **/
    function saveMenuPower() {
        try {
            var checkStatus = layui.table.checkStatus('list').data;
            var gxStr = [];
            for (var i = 0; i < checkStatus.length; i++) {
                gxStr.push(checkStatus[i].pCode);
            }
            gxStr = gxStr.join(',');//必须要写，不然后台获取不到数据
            $.ajax({
                url: "../System/saveMenuPower",
                data: { menuCode: mCode, powerValue: gxStr },
                dataType: "json",
                type: "POST",
                success: function (data) {
                    if (data.result) {
                      layer.msg("保存成功");
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        } catch (e) {
            window.location = "/Error/Error";
        }
    }

    function SaveSinglePower(dataId,fields,value){
        $.ajax({
            url: "../System/SaveSingleWebPower",
            data: { dataId: dataId, field: fields,value:value },
            dataType: "json",
            type: "POST",
            success: function (data) {
                if (data.result) {
                    layer.alert("保存成功", { icon: 6 });
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }
    
    function DelWebPower(dataId) {
        $.ajax({
            url: "../System/DelWebPower",
            data: { dataId: dataId},
            dataType: "json",
            type: "POST",
            success: function (data) {
                if (data.result) {
                  loadData();
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }
</script>
